package part1.language_basis.day05;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

//数组的演示
public class ArrayDemo {
    public static void main(String[] args) {
        //数组的排序：
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random()*100);
            System.out.println(arr[i]);
        }
        Arrays.sort(arr); //对arr数组做升序排序
        /*
        //实现数组反转顺序
        List list = Arrays.asList(arr);
        Collections.reverse(list);
         */
        System.out.println("排序后");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        System.out.println("倒着输出");
        for (int i = arr.length-1; i >= 0 ; i--) {
            System.out.println(arr[i]);
        }

        System.out.println("数组第1个元素值为:"+arr[0]);


        /*
        //4)数组的遍历：从头到尾挨个走一遍
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) { //遍历arr数组
            arr[i] = (int) (Math.random()*100);
            System.out.println(arr[i]);
        }
        for (int a:arr) {
            System.out.println(a);
        }

         */


        /*
        //1)数组的定义
        //声明整型数组a，包含4个元素，每个元素都是int类型，默认值为0
        int[] a = new int[4];
        boolean[] b = new boolean[5];
        double[] c = new double[6];

        //2)数组的初始化---初始化数组中的元素
        int[] arr1 = new int[3];
        int[] arr2 = {12, 45, 87};
        int[] arr3 = new int[]{2, 51, 65, 7};
        //int[] arr = new int[3]{1,2,3}; 编译错误
        int[] arr4;
        //arr4 = {12,55}; 编译错误 此方式只能声明同时初始化
        arr4 = new int[]{23, 56, 88};

        //3)数组的访问-----访问的是数组的中的元素
        int[] arr = new int[3];
        System.out.println("数组的长度："+arr.length); //3
        System.out.println(arr[0]); //0，输出第1个元素
        arr[0] = 100; //给第1个元素赋值为100
        arr[1] = 200; //给第2个元素赋值为200
        arr[2] = 300; //给第3个元素赋值为300
        //arr[3] = 400; //运行时会发生数组下标越界异常
        System.out.println(arr[arr.length-1]);//300，输出最后一个元素的值


        //二维数组
        int[][] arr5 = new int[3][2];
        int[][] arr6 = {{1,3},{3,4},{3,3,6}};
        System.out.println(arr6[2][2]);
        for (int i = 0; i < arr6.length; i++) {
            for (int j = 0; j < arr6[i].length; j++) {
                System.out.println("--"+arr6[i][j]);
            }
        }

         */

    }
}
